package com.foreverwin.jzymes.common.service.impl;

import com.foreverwin.jzymes.common.mapper.CustomFieldsAdviceMapper;
import com.foreverwin.jzymes.common.service.ICustomFieldsAdviceService;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CustomFieldsAdviceServiceImpl implements ICustomFieldsAdviceService {

    @Autowired
    CustomFieldsAdviceMapper customFieldsAdviceMapper;

    /**
     * 自定义字段值是否存在
     *
     * @param handle
     * @param customAttr
     * @return
     */
    public String existCustomValue(String handle, String customAttr ){
        String customValue = findCustomValue( handle, customAttr );
        Assert.hasText( customValue, I18nUtil.getI18nText( "common.customFieldValueNotExist", new String[]{ handle, customAttr } ) );
        return customValue;
    }

    /**
     * 查询自定义字段值
     *
     * @param handle
     * @param customAttr
     * @return
     */
    public String findCustomValue(String handle, String customAttr ){
        Assert.hasText( handle, I18nUtil.getI18nText( "common.customFieldHandleNotBlank" ));
        Assert.hasText( customAttr, I18nUtil.getI18nText( "common.customFieldAttributeNotBlank" ) );

        String customValue = customFieldsAdviceMapper.selectCustomValue( handle, customAttr );
        return customValue;
    }


    /**
     * 通过自定义字段值获取自定义字段上下文
     *
     * @param site
     * @param customAttr
     * @param customValue
     * @return
     */
    public String findCustomFieldHandle( String site, String customAttr, String customValue ){
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        Assert.hasText( customAttr, I18nUtil.getI18nText( "common.customFieldAttributeNotBlank" ) );
        Assert.hasText( customValue, I18nUtil.getI18nText( "common.customFieldValueNotBlank" ) );

        String handle = null;
        List<Map<String,String>> customFieldList = customFieldsAdviceMapper.selectCustomFieldByValue( customAttr, customValue );
        for( Map<String,String> customFieldMap : customFieldList ){
            handle = customFieldMap.get( "HANDLE" );
            if( handle.split( "," ).length > 0 ){
                String buff = handle.split( "," )[0];
                if( buff.endsWith( site ) ){
                    return handle;
                }
            }
        }
        //Assert.hasText( handle, I18nUtil.getI18nText( "common.customFieldNotExist", new String[]{ site, customAttr, customValue } ) );
        return null;
    }

    /**
     * 通过自定义字段值获取自定义字段上下文列表
     *
     * @param site
     * @param customAttr
     * @param customValue
     * @return
     */
    public List<String> findCustomFieldHandleList( String site, String customAttr, String customValue ){
        List<String> handleList = new ArrayList<>();
        List<Map<String,String>> customFieldList = customFieldsAdviceMapper.selectCustomFieldByValue( customAttr, customValue );
        for( Map<String,String> customFieldMap : customFieldList ){
            String handle = customFieldMap.get( "HANDLE" );
            if( handle.split( "," ).length > 0 ){
                String buff = handle.split( "," )[0];
                if( buff.endsWith( site ) ){
                    handleList.add( handle );
                }
            }
        }
        return handleList;
    }

    /**
     * 查询自定义数据
     *
     * @param site
     * @param customAttr
     * @return
     */
    public List<Map<String,String>> findCustomFieldList( String site, String customAttr ){
        List<Map<String,String>> customFields = new ArrayList<>();
        List<Map<String,String>> customFieldList = customFieldsAdviceMapper.selectCustomFieldByAttribute( customAttr );
        for( Map<String,String> customFieldMap : customFieldList ){
            String handle = customFieldMap.get( "HANDLE" );
            if( handle.split( "," ).length > 0 ){
                String buff = handle.split( "," )[0];
                if( buff.endsWith( site ) ){
                    customFields.add( customFieldMap );
                }
            }
        }
        return customFields;
    }

    /**
     * 更新自定义字段
     *
     * @param handle
     * @param customAttr
     * @param customValue
     * @return
     */
    public int  updateCustomField( String handle, String customAttr, String customValue ){
        Assert.hasText( handle, I18nUtil.getI18nText( "common.customFieldHandleNotBlank" ));
        Assert.hasText( customAttr, I18nUtil.getI18nText( "common.customFieldAttributeNotBlank" ) );

        String value = customFieldsAdviceMapper.selectCustomValue( handle, customAttr );
        if( value == null ){
            Assert.hasText( customValue, I18nUtil.getI18nText( "common.customFieldValueNotBlank" ) );
            return customFieldsAdviceMapper.insertCustomValue( handle, customAttr, customValue );
        }else{
            return customFieldsAdviceMapper.updateCustomValue( handle, customAttr, customValue );
        }
    }

    /**
     * 插入自定义字段
     *
     * @param handle
     * @param customAttr
     * @param customValue
     */
    public void insertCustomField( String handle, String customAttr, String customValue ){
        Assert.hasText( handle, I18nUtil.getI18nText( "common.customFieldHandleNotBlank" ));
        Assert.hasText( customAttr, I18nUtil.getI18nText( "common.customFieldAttributeNotBlank" ) );
        Assert.hasText( customValue, I18nUtil.getI18nText( "common.customFieldValueNotBlank" ) );

        customFieldsAdviceMapper.insertCustomValue( handle, customAttr, customValue );
    }
}
